package com.gable.exhibition.service.impl;

import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.UUIDGenerator;
import com.gable.exhibition.dao.TrainDailyDao;
import com.gable.exhibition.dao.TrainDao;
import com.gable.exhibition.dao.TrainFileDao;
import com.gable.exhibition.dao.TrainPersonDao;
import com.gable.exhibition.service.TrainService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.TrainDailyView;
import com.gable.exhibition.view.TrainFileView;
import com.gable.exhibition.view.TrainPersonView;
import com.gable.exhibition.view.TrainView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by SJX on 2018/3/6.
 */
@Service("trainService")
public class TrainServiceImpl implements TrainService {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    TrainDao trainDao;

    @Autowired
    TrainPersonDao trainPersonDao;

    @Autowired
    TrainDailyDao trainDailyDao;

    @Autowired
    TrainFileDao trainFileDao;

    @Override
    public ReturnResultUtil queryTrainList(TrainView trainView) {
        int count = trainDao.queryTrainCount(trainView);
        List<TrainView> list = trainDao.queryTrainList(trainView);
        TrainView trainView1 = new TrainView();
        trainView1.setCount(count);
        trainView1.setList(list);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,trainView1);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil addTrain(TrainView trainView) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        trainView.setTrainId(UUIDGenerator.getUniqueLong());
        trainView.setCreateDate(df.format(new Date()));
        trainView.setIsDel(0);
        trainDao.insertSelective(trainView);

        List<TrainPersonView> personViews = trainView.getPersonList();
        if(personViews!=null && !personViews.isEmpty()){
            for(TrainPersonView personView:personViews){
                TrainPersonView tp = new TrainPersonView();
                tp.setTrainId(trainView.getTrainId());
                trainPersonDao.insertSelective(tp);
            }
        }

        List<TrainDailyView> dailyViews = trainView.getDailyList();
        if(dailyViews!=null && !dailyViews.isEmpty()){
            for(TrainDailyView dailyView:dailyViews){
                TrainDailyView td = new TrainDailyView();
                trainDailyDao.insertSelective(td);
            }
        }

        List<TrainFileView> fileViews = trainView.getFileList();
        if(fileViews!=null && !fileViews.isEmpty()){
            for(TrainFileView fileView:fileViews){
                TrainFileView tf = new TrainFileView();
                trainFileDao.insertSelective(tf);
            }
        }

        return new ReturnResultUtil(200, MessageUtil.SAVESUCCESS,trainView);
    }

    @Override
    public ReturnResultUtil queryTrainInfo(Long trainId) {
        TrainView tv = trainDao.queryTrainInfo(trainId);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,tv);
    }
}
